//
//  NSString+StringSize.m
//  SecondRod
//
//  Created by Apple on 2016/12/9.
//  Copyright © 2016年 ZGA. All rights reserved.
//

#import "NSString+StringSize.h"

@implementation NSString (StringSize)


/**
   单行计算文本宽度

 @param font 字体大小
 @return CGSize
 */
- (CGSize)sizeSingleWithLabelFont:(CGFloat)font{
    NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:font]};
    return [self sizeWithAttributes:dict];;
}

/**
   计算多行文本，没有设置行间距 文本的高多

 @param width 文本宽度
 @param font  字体大小
 @return CGSize
 */
- (CGSize)sizeWithLabelWidth:(CGFloat)width font:(UIFont *)font{
    NSDictionary *dict = @{NSFontAttributeName : font};
    
    CGRect rect = [self boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    
    return CGSizeMake(sizeWidth, sizeHieght);
}

/**
 计算多行文本，设置行间距 文本的高多

 @param lineSpacing 行间距
 @param width 文本宽度
 @param fontSize 字体大小
 @return CGSize
 */
- (CGSize)sizeWithLabelLineSpacing:(CGFloat)lineSpacing width:(CGFloat)width FontSize:(CGFloat)fontSize{
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    
    paraStyle.alignment = NSTextAlignmentLeft;
    
    paraStyle.lineSpacing = lineSpacing; //行间距
    
    paraStyle.hyphenationFactor = 1.0;
    
    paraStyle.firstLineHeadIndent = 0.0;
    
    paraStyle.paragraphSpacingBefore = 0.0;
    
    paraStyle.headIndent = 0;
    
    paraStyle.tailIndent = 0;
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    CGRect rect = [self boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    
    return CGSizeMake(sizeWidth, sizeHieght);
}

@end
